package www.rlicker.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.OperationLog;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IOperationLogService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.ExportExcel;
import www.rlicker.utils.StringUtil;

@Controller
@RequestMapping(value="/operationlog")
public class operationLogController extends BaseController{

	@Autowired
	private IOperationLogService service;
	
	@RequestMapping("/showlist")
	public String showList(){
		return "system/operationLogList";
	}
	
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
	   Criteria<OperationLog> param = new Criteria<OperationLog>();
	   if (!StringUtil.isNullOrBlank(request.getParameter("userName"))) {
			param.addParam("user_name", request.getParameter("userName"));
	   }
       if (!StringUtil.isNullOrBlank(request.getParameter("userId"))) {
          	param.addParam("user_id", request.getParameter("userId"));
       }
       if (!StringUtil.isNullOrBlank(request.getParameter("startTime"))) {
        	param.addParam("startTime", request.getParameter("startTime"));
       }
       if (!StringUtil.isNullOrBlank(request.getParameter("endTime"))) {
    	   param.addParam("endTime", request.getParameter("endTime"));
       }
       if (!StringUtil.isNullOrBlank(request.getParameter("ip"))) {
    	   param.addParam("ip", request.getParameter("ip"));
       }
       if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
    	   param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
       }
       if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
    	   param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
       }
	   List<Map<String,Object>> list = service.queryPage(param);
	   PageResult pageResult = new PageResult();
	   pageResult.setTotal(service.queryPageCount(param));
	   pageResult.setRows(list);
	   return pageResult;	
	}
	
	/** 
	 * 导出excel 
	 * @return 
	 */  
	@ResponseBody
	@RequestMapping(value = "/export")
	public String excelPage(HttpServletResponse response){
		Criteria<OperationLog> param = new Criteria<OperationLog>();
		List<Map<String,Object>> list = service.queryPage(param);
	    String header="编号:id,时间:createTime,姓名:userName,帐号:userId,IP:ip,操作内容:content";
	    ExportExcel.excel(response, list, "操作日志列表", header);
	    return "success";
	}    
}
